from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey

# 在本文件下运行该文件需要去掉路径中的db/
engine = create_engine('sqlite:///db/image.db?check_same_thread=False', encoding='utf-8', echo=True)
Base = declarative_base()


class Texture(Base):
    __tablename__ = 'texture'
    id = Column(Integer, primary_key=True)
    path = Column(String(200))
    R = Column(Integer)
    G = Column(Integer)
    B = Column(Integer)
    type = Column(String(20))

    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


class Edge(Base):
    __tablename__ = 'edge'
    id = Column(Integer, primary_key=True)
    path = Column(String(200))

    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


class Point(Base):
    __tablename__ = 'point'
    id = Column(Integer, primary_key=True)
    edge_id = Column(Integer, ForeignKey('edge.id'))
    x = Column(Integer)
    y = Column(Integer)

    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


if __name__ == '__main__':
    # 创建数据表
    Base.metadata.create_all(engine)
